package lego.jbv;

import lego.Moving.Car;
import lego.animal.IAnimal;
import lejos.util.Delay;

public class Hunger extends Thread{
	private boolean loseLife = false;
	private boolean stopNow;
	private IAnimal animal;
	private SoundController soundController;
	private boolean eating = false;
	
	public Hunger(IAnimal animal, SoundController soundController)
	{
		this.animal = animal;
		this.soundController = soundController;
		this.setDaemon(true);
		stopNow = false;
	}
	
	public void startEating()
	{
		eating = true;
	}
	
	public void stopEating()
	{
		eating = false;
	}
	
	public void run()
	{
		while(!stopNow)
		{
			if(eating)
			{
				Display.setStamina(Display.getStamina()+1);
				soundController.playSound(animal.getMiss());
				
				if(loseLife)
					Display.setLife(Display.getLife()+1);
				
				Delay.msDelay(250);
			}
			else{
				Delay.msDelay(500);
				Display.setStamina(Display.getStamina()-1);
				
				if(Display.getStamina() <= 0)
				{
					Display.setStatus("YOU ARE HUNGRY");
					Car.stop();
					break;
				}
				
				if(loseLife)
					Display.setLife(Display.getLife()-1);
				
				
			}
			loseLife = !loseLife;
		}
	}

	public void stop()
	{
		stopNow = true;
	}
}
